package top.yqingyu.trans$server.main;

import lombok.extern.slf4j.Slf4j;
import top.yqingyu.common.utils.InitUtil;
import top.yqingyu.qymsg.netty.MsgServer;
import top.yqingyu.trans$server.event$handler.MainHandler;
import top.yqingyu.trans$server.event$handler.S2CtHandler;
import top.yqingyu.trans$server.exception.ExceptionHandle;
import top.yqingyu.trans$server.thread.ClientMonitorThread;
import top.yqingyu.trans$server.trans.ClientTransThread;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

/**
 * @author YYJ
 * @version 1.0.0
 * @ClassName top.yqingyu.main.Start
 * @description
 * @createTime 2022年09月07日 15:40:00
 */
@Slf4j
public class Start {
    public static void main(String[] args) throws Exception {
        MainConfig.banner();
        log.info("loading main config");
        MainConfig.load();

        log.info("loading s$ct config");
        S$CtConfig.load();

        log.info("starting main server");
        InitUtil.init("top.yqingyu.trans$server");

        MsgServer main = new MsgServer.Builder()
                .serverName("Main")
                .threadName("handler")
                .bodyLengthMax(MainConfig.MSG_BODY_LENGTH_MAX)
                .handler(MainHandler.class)
                .exceptionHandler(new ExceptionHandle())
                .build();
        main.start(MainConfig.PORT_MAIN);
        MainConfig.msgTransfer = main.getMsgTransfer();

        log.info("starting s$ct server");
        MsgServer sc = new MsgServer.Builder()
                .serverName("S2Ct")
                .threadName("handler")
                .pool(8)
                .bodyLengthMax(MainConfig.MSG_BODY_LENGTH_MAX)
                .handler(S2CtHandler.class)
                .exceptionHandler(new ExceptionHandle())
                .build();
        sc.start(MainConfig.PORT_INTER);
        S$CtConfig.msgTransfer = sc.getMsgTransfer();

        ClientTransThread.init();
        ClientMonitorThread.init();

        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
        long startTime = runtime.getStartTime();
        log.info("START SUCCESS PID:{} in {} mills ", runtime.getPid(), System.currentTimeMillis() - startTime);
        main.block();
    }
}
